package org.jeecg.modules.mg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.mg.DateUtil;
import org.jeecg.modules.mg.entity.StockDaily;
import org.jeecg.modules.mg.entity.StockDailyFactor;
import org.jeecg.modules.mg.mapper.StockDailyMapper;
import org.jeecg.modules.mg.service.IStockDailyFactorService;
import org.jeecg.modules.mg.service.IStockDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dinggang
 * @since 2021-02-17
 */
@Service
public class StockDailyServiceImpl extends ServiceImpl<StockDailyMapper, StockDaily> implements IStockDailyService {

    @Autowired
    private IStockDailyFactorService stockDailyFactorService;

    /**
     * fsdfff的
     * @param code
     * @return
     */
    @Cacheable(value = "daily")
    @Override
    public List<List<Object>> getByCode(String code) {
        List<StockDaily> list = list(new LambdaQueryWrapper<StockDaily>().eq(StockDaily::getTsCode,code).orderByAsc(StockDaily::getTradeDate));
        List<StockDailyFactor> factors = stockDailyFactorService.list(new LambdaQueryWrapper<StockDailyFactor>().eq(StockDailyFactor::getTsCode,code).orderByDesc(StockDailyFactor::getId));
        Map<String,Double> factorMap = new HashMap<>();
        Double currentFactor = factors.get(0).getAdjFactor();
        for (StockDailyFactor factor:factors             ) {
            factorMap.put(factor.getTradeDate(),factor.getAdjFactor());
        }
        List<List<Object>> data = new ArrayList<>(list.size());
        for (StockDaily daily:list) {
            List<Object> dailyData = new ArrayList<>();
            String date = daily.getTradeDate();
            Double factorTemp = factorMap.get(date)/currentFactor;
            dailyData.add(DateUtil.parseDateString(date));
            dailyData.add(daily.getOpen() * factorTemp);
            dailyData.add(daily.getClose() * factorTemp);
            dailyData.add(daily.getLow() * factorTemp);
            dailyData.add(daily.getHigh() * factorTemp);
            dailyData.add(daily.getVol());
            data.add(dailyData);
        }
        return data;
    }
}
